package com.hospital.service.impl;

import com.hospital.dao.DepartmentDao;
import com.hospital.entity.Department;
import com.hospital.service.intf.DepartmentService;
import com.hospital.util.StringUtil;
import com.hospital.vo.PageBean;
import com.hospital.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service()
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentDao departmentDao;

    @Override
    public R add(Department department) {
        if(departmentDao.add(department) > 0){
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public R delete(Integer id) {
        if(departmentDao.delete(id) > 0){
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public R update(Department department) {
        if(departmentDao.update(department) > 0){
            return R.ok();
        }
        return R.fail();
    }

    @Override
    public R select() {
        if(departmentDao.select() != null){
            return R.ok(departmentDao.select());
        }
        return R.fail();
    }

    @Override
    public PageBean selectPage(String department_name, int page, int limit) {
        int start = (page - 1) * limit;

        if(StringUtil.isNotEmpty(department_name)){
            department_name = "%"+department_name+"%";
        }

        return PageBean.ok(departmentDao.count(department_name),departmentDao.selectPage(department_name,start,limit));
    }

    @Override
    public PageBean selectPageDepartmentEmployee(String department_name, int page, int limit) {
        int start = (page - 1) * limit;

        if(StringUtil.isNotEmpty(department_name)){
            department_name = "%"+department_name+"%";
        }

        return PageBean.ok(departmentDao.count(department_name),departmentDao.selectPageDepartmentEmployee(department_name,start,limit));
    }
}
